package bsts.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import bsts.model.Payment;
import bsts.model.Transaction;

public class TransactionController {

	// Method skeleton done by Than Htet Aung : 20/3/2011
	// Method logic and statement started by Franscis : 20/3/2011
	public ArrayList<Transaction> getCallTransaction(long subscriptionId,
			long featureid, Date startDate, Date endDate) {
		return null;
	}

	public long getTotalCallDuration(ArrayList<Transaction> transactions) {
		long totalDurationInSecond = 0;
		for (Transaction t : transactions)
			totalDurationInSecond += t.getDuration();
		return totalDurationInSecond;
	}

	private long transactionId = 201103210002L;
	private IOController myio = new IOController();

	public ArrayList<Transaction> getTransactions(long subscriptionId,
			long featureid, Date startDate, Date endDate) {
		ArrayList<Transaction> myinTransactions = myio.RetrieveTransactions();
		ArrayList<Transaction> myoutTransactions = new ArrayList<Transaction>();
		if (myinTransactions != null) {
			Iterator<Transaction> i = myinTransactions.iterator();
			int size = myinTransactions.size();
			while (i.hasNext()) {
				Transaction transaction = i.next();
				if (transaction.getSubscriptionId() == subscriptionId) {
					if (transaction.getFeatureId() == featureid) {
						Date tranStartdate = transaction
								.getStartDateTimeofCall();
						if ((tranStartdate.after(startDate))
								&& (tranStartdate.before(endDate))) {
							if (!myoutTransactions.add(transaction)) {
								System.out
										.println("Adding transaction to transaction list from TransactionController class failed");
							}
						}
					}
				}
			}
			if (size == 0) {
				return null;
			}
		}
		return myoutTransactions;
	}

}
